package com.lc.utils;

import com.lc.pojo.UmsPcdDic;

import java.util.ArrayList;
import java.util.List;

/**
 * @description 树形结构递归工具
 */
public class TreeUtil {

    // 建立父节点树形结构
    public static List<UmsPcdDic> buildTree(List<UmsPcdDic> depts){
        ArrayList<UmsPcdDic> sysDepts = new ArrayList<>();
        for (UmsPcdDic sysDept : getNodeTree(depts)) {
            getChildrenNode(depts, sysDept);
            sysDepts.add(sysDept);
        }
        return sysDepts;
    }
    // 查询所有的父节点，getParent()=0
    private static List<UmsPcdDic> getNodeTree(List<UmsPcdDic> depts){
        ArrayList<UmsPcdDic> sysDepts = new ArrayList<>();
        for (UmsPcdDic dept : depts) {
            if (dept.getParentId()==0){
                sysDepts.add(dept);
            }
        }
        return sysDepts;
    }

    // 递归建立树形子节点，将子节点放到父节点下
    private static UmsPcdDic getChildrenNode(List<UmsPcdDic> depts,UmsPcdDic sysDept) {
        ArrayList<UmsPcdDic> sysDepts = new ArrayList<>();
        for (UmsPcdDic dept : depts) {
            if (dept.getParentId().equals(sysDept.getId())) {
                sysDepts.add(getChildrenNode(depts, dept));
            }
        }
        sysDept.setPcdList(sysDepts);
        return sysDept;
    }

}